package com.example.wangzh.developkit.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.utils.UIUtils;
import com.example.wangzh.developkit.utils.ViewUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by USER on 2017/1/22.
 */

public class PwdInputer extends LinearLayout {
    EditText edt;
    CheckBox ckb;
    String hint;
    int textColor;
    int hintTextColor;
    float text_margin_left;
    float text_margin_top;
    float text_margin_bottom;
    float text_size;


    @BindView(R.id.lnl_ckb)
    LinearLayout lnl_ckb;
    @BindView(R.id.img)
    ImageView img;

    public PwdInputer(Context context) {
        super(context);
        initView(context, null);
    }


    public PwdInputer(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
        getEditableText();
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_pwd_inputer, this);
        ButterKnife.bind(this, view);
        edt = (EditText) view.findViewById(R.id.edt);
        ckb = (CheckBox) findViewById(R.id.ckb);
        textColor = edt.getCurrentTextColor();
        hintTextColor = edt.getCurrentHintTextColor();
        if (isInEditMode()) {
            return;
        }
        loadAttrs(context, attrs);
        edt.setTypeface(Typeface.MONOSPACE);
        lnl_ckb.setOnClickListener(new OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           ckb.setChecked(!ckb.isChecked());
                                           if (ckb.isChecked()) {
                                               edt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                                               //切换该输入类型时有可能会把字体改为带空格的样式Typeface.MONOSPACE
                                           } else {
                                               edt.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                                               //切换该输入类型时有可能会把字体改为不带空格的样式Typeface.DEFAULT
                                               edt.setTypeface(Typeface.MONOSPACE);
                                           }
                                           edt.setSelection(edt.getText().length());
                                       }
                                   }

        );
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edt.setText("");
            }
        });
        edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ViewUtils.visibleOrInvisible(img, s.length() != 0);
            }
        });
        edt.setTextColor(textColor);
        edt.setHintTextColor(hintTextColor);
        LinearLayout.LayoutParams lp = (LayoutParams) edt.getLayoutParams();
        lp.leftMargin = (int) text_margin_left;
        lp.topMargin = (int) text_margin_top;
        lp.bottomMargin = (int) text_margin_bottom;
        edt.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_size);

    }

    private void loadAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PwdInputer);
        hint = a.getString(R.styleable.PwdInputer_pwi_hint);
        if (hint != null) {
            edt.setHint(hint);
        }
        textColor = a.getColor(R.styleable.PwdInputer_pwi_textColor, textColor);
        hintTextColor = a.getColor(R.styleable.PwdInputer_pwi_hintTextColor, hintTextColor);
        text_margin_left = a.getDimensionPixelSize(R.styleable.PwdInputer_pwi_text_margin_left, UIUtils.dip2Px(0));
        text_margin_top = a.getDimensionPixelSize(R.styleable.PwdInputer_pwi_text_margin_top, UIUtils.dip2Px(11));
        text_margin_bottom = a.getDimensionPixelSize(R.styleable.PwdInputer_pwi_text_margin_top, UIUtils.dip2Px(11));
        text_size = a.getDimensionPixelSize(R.styleable.PwdInputer_pwi_text_size, UIUtils.dip2Px(14));
    }

    public void setHint(String hint) {
        if (hint == null) {
            return;
        }
        this.hint = hint;
        edt.setHint(hint);
    }

    public Editable getEditableText() {
        return edt.getEditableText();
    }

    public EditText getEdt() {
        return edt;
    }
}
